package cn.ffcs.uec.recordsApproval.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.recordsApproval.action.bean.RecordsApprovalViewMainBean;
import cn.ffcs.uec.recordsApproval.constant.ApprovalConstants;
import cn.ffcs.uec.staff.model.UecEnc;
import cn.ffcs.uec.staff.model.UecPsnjob;
import cn.ffcs.uec.staff.model.UecPunish;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uec.staff.model.UecTalentTeam;
import cn.ffcs.uec.staff.model.UecTrial;
import cn.ffcs.uec.staffRecords.vo.AtiOaBaseObject;
import cn.ffcs.uom.common.model.DefaultDaoFactory;
import cn.ffcs.uom.common.util.PubUtil;

@Controller
@Scope("prototype")
public class RecordApprovalViewComposer extends BasePortletComposer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3260345305026957231L;
	
	private RecordsApprovalViewMainBean bean = new RecordsApprovalViewMainBean();
	
	private Object object;
	
	private Object objectHis;
	
	private String taskDefKey;
	
	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}
	
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}
	
	
	/**
	 * 界面初始化
	 */
	public void onCreate$recordApprovalViewWindow() {
		
		setAllExtUnvisible();
		bindBean();
		bindCombobox();
	}
	

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getRecordApprovalViewWindow().onClose();
	}
	
	/**
	 * 审批同意
	 */
	public void onApproval() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("comment", this.getComment(taskDefKey));
		map.put("onApprovalResult", ApprovalConstants.ON_APPROVAL_RESULT);
		Events.postEvent("onApprovalResponse", this.self, map);
		this.onCancel();
	}
	
	/**
	 * 审批驳回
	 */
	public void onReject() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("comment", this.getComment(taskDefKey));
		map.put("onRejectResult", ApprovalConstants.ON_REJECT_RESULT);
		Events.postEvent("onRejectResponse", this.self, map);
		this.onCancel();
	}
	
	/**
	 * 重新申请
	 */
	public void onOk() {
		
		if(object instanceof UecOutStaffInfo) {
			PubUtil.fillPoFromEditorBean(object, this.bean.getOutStaffEditApprovalViewExt().getBean());
			DefaultDaoFactory.getDefaultDao().updateObject(object);
		}
		
		
		Events.postEvent("onOKResponse", this.self, ApprovalConstants.ON_OK_RESULT);
		this.onCancel();
	}
	
	public void bindBean() {

		String procDefName = (String) arg.get("procDefName");
		this.bean.getRecordApprovalViewWindow().setTitle(procDefName + "审批");
		
		taskDefKey = (String) arg.get("taskDefKey");
		if("modify".equals(taskDefKey)) {
			setButtonValid(true, false, false);
		}else {
			setButtonValid(false, true, true);
		}
		if("deptLeaderAudit".equals(taskDefKey)) {
			this.bean.getSecondText().setRows(2);
			this.setTextValid(true, false, false);
		}
		if("hrAudit".equals(taskDefKey)) {
			this.bean.getThirdText().setRows(2);
			this.setTextValid(false, true, false);
		}
		if("hrLeaderAudit".equals(taskDefKey)) {
			this.bean.getFourthText().setRows(2);
			this.setTextValid(false, false, true);
		}
		
		this.setExtVisible();
		
	
	}
	
	public void bindCombobox() {
		
	}
	
	
	
	/**
	 * 设置按钮
	 * @param canEdit
	 * @param canApproval
	 * @param canReject
	 */
	private void setButtonValid( boolean canEdit, boolean canApproval, boolean canReject) {
		this.bean.getOkBtn().setVisible(canEdit);
		this.bean.getOkBtn().setDisabled(!canEdit);
		
		this.bean.getApprovalBtn().setVisible(canApproval);
		this.bean.getApprovalBtn().setDisabled(!canApproval);
		
		this.bean.getRejectBtn().setVisible(canReject);
		this.bean.getRejectBtn().setDisabled(!canReject);
		
	}
	
	/**
	 * 审批意见设置可见
	 * @param secondCanEdit
	 * @param thirdCanEdit
	 * @param fourthCanEdit
	 */
	private void setTextValid(boolean secondCanEdit, boolean thirdCanEdit, boolean fourthCanEdit) {
		this.bean.getSecondText().setDisabled(!secondCanEdit);
		this.bean.getThirdText().setDisabled(!thirdCanEdit);
		this.bean.getFourthText().setDisabled(!fourthCanEdit);
	}
	
	private void setExtVisible() {
		objectHis = arg.get("objectHis");
		object = arg.get("object");
		//入职流程可见
		if(object instanceof UecStaffInfo) {
			this.bean.getStaffEntryApprovalViewExt().setVisible(true);
			if (null != object) {
				PubUtil.fillBeanFromPo(object, bean.getStaffEntryApprovalViewExt().getBean());	
			}
		}
		//外包转正流程可见
		if(object instanceof UecTrial) {
			this.bean.getOutStaffTrialApprovalViewExt().setVisible(true);
			if (null != object) {
				PubUtil.fillBeanFromPo(object, bean.getOutStaffTrialApprovalViewExt().getBean());	
			}
		}
//		if("outStaffTrial".equals(procDefKey)) {
//			this.bean.getOutStaffTrialApprovalViewExt().setVisible(true);
//			object = (UecTrial) arg.get("object");
//			if (null != object) {
//				PubUtil.fillBeanFromPo(object, bean.getOutStaffTrialApprovalViewExt().getBean());	
//			}
//		}
		//外包人员信息修改流程可见
		if(object instanceof UecOutStaffInfo) {
			this.bean.getOutStaffEditApprovalViewExt().setVisible(true);
			PubUtil.fillBeanFromPo(object, bean.getOutStaffEditApprovalViewExt().getBean());
			PubUtil.fillBeanFromPo(objectHis, bean.getOutStaffEditApprovalViewExt().getBean());
		}
		//外包人员惩罚信息流程可见
		if(object instanceof UecPunish) {
			this.bean.getOutStaffPunishEditApprovalViewExt().setVisible(true);
			PubUtil.fillBeanFromPo(object, bean.getOutStaffPunishEditApprovalViewExt().getBean());
		}
		//奖励信息弹框可见
		if(object instanceof UecEnc) {
			this.bean.getOutStaffEncEditApprovalViewExt().setVisible(true);
			PubUtil.fillBeanFromPo(object, bean.getOutStaffEncEditApprovalViewExt().getBean());
		}
		//专家人才队伍
		if(object instanceof UecTalentTeam) {
			this.bean.getOutStaffTalentTeamEditApprovalViewExt().setVisible(true);
			PubUtil.fillBeanFromPo(object, bean.getOutStaffTalentTeamEditApprovalViewExt().getBean());
		}
		//外包人员兼职
		if(object instanceof UecPsnjob) {
			this.bean.getOutStaffPsnjobEditApprovalViewExt().setVisible(true);
			PubUtil.fillBeanFromPo(object, bean.getOutStaffPsnjobEditApprovalViewExt().getBean());
		}
		
		this.bean.getOutStaffEditApprovalViewExt().setVisible(true);
		PubUtil.fillBeanFromPo(object, bean.getOutStaffEditApprovalViewExt().getBean());
		PubUtil.fillBeanFromPo(objectHis, bean.getOutStaffEditApprovalViewExt().getBean());
		
		AtiOaBaseObject oaBaseObject = (AtiOaBaseObject) arg.get("oaBaseObject");
		PubUtil.fillBeanFromPo(oaBaseObject, bean);
		

		
	}
	
	private void setAllExtUnvisible() {
		
		this.bean.getStaffEntryApprovalViewExt().setVisible(false);
		this.bean.getOutStaffTrialApprovalViewExt().setVisible(false);
		this.bean.getOutStaffEditApprovalViewExt().setVisible(false);
		this.bean.getOutStaffPunishEditApprovalViewExt().setVisible(false);
		this.bean.getOutStaffEncEditApprovalViewExt().setVisible(false);
		this.bean.getOutStaffTalentTeamEditApprovalViewExt().setVisible(false);
		this.bean.getOutStaffPsnjobEditApprovalViewExt().setVisible(false);
		
		this.setTextValid(false, false, false);
		
		
		
	}
	
	/**
	 * 获取审批意见
	 * @param taskDefKey
	 * @return
	 */
	public String getComment(String taskDefKey) {
		if("deptLeaderAudit".equals(taskDefKey)) {
			return this.bean.getSecondText().getValue();
		}
		if("hrAudit".equals(taskDefKey)) {
			return this.bean.getThirdText().getValue();
		}
		if("hrLeaderAudit".equals(taskDefKey)) {
			return this.bean.getFourthText().getValue();
		}
		return null;
	}
	
	/**
	 * 页面bean控件设置可见
	 */
	@SuppressWarnings("unused")
    private void setAllExtDisable() {
		List<Component> list = this.bean.getStaffEntryApprovalViewExt().getChildren();
		
	}
}
